import json
import os

def add_todo():
    # 输入待办事项的详细信息
    title = input("Enter your title: ")
    description = input("Enter your description: ")
    priority = input("Enter your priority: ")
    started_at = input("Enter your started_at: ")
    finished_at = input("Enter your finished_at: ")

    new_todo = {
        "title": title,
        "description": description,
        "done": False,
        "started_at": started_at,
        "finished_at": finished_at,
        "priority": priority
    }

    file_path = 'todo.json'
    if os.path.exists(file_path):
        with open(file_path, 'r') as f:
            todos = json.load(f)
    else:
        todos = []

    todos.append(new_todo)

    with open(file_path, 'w') as f:
        json.dump(todos, f, indent=4)
    print("TODO added successfully.")
    return todos


def delete_todo():
    file_path = 'todo.json'
    if os.path.exists(file_path):
        with open(file_path, 'r') as f:
            todos = json.load(f)
        # 假设我们要删除第一个待办事项，这里应该添加删除逻辑
        todos.pop()  # 删除第一个待办事项
        # 这里只是示例，实际删除逻辑可能更复杂
        with open(file_path, 'w') as f:
            json.dump(todos, f, indent=4)
        print("TODO deleted successfully.")
    return todos  # 返回更新后的待办事项列表import json
import os
def add_todo():
    todos = []  # 改为列表存储多个待办事项
    # 输入待办事项的标题
    title = input("Enter your title: ")
    # 输入待办事项的描述
    description = input("Enter your description: ")
    # 输入待办事项的优先级
    priority = input("Enter your priority: ")
    started_at = input("Enter your started_at: ")
    finished_at = input("Enter your finished_at: ")



    # 创建新的待办事项字典
    new_todo = {  # 创建新的待办事项字典
        "title": title,
        "description": description,
        "done": False,
        "started_at": started_at,
        "finished_at": finished_at,
        "priority": priority
    }

    # 检查文件是否存在，并相应地读取或初始化待办事项列表
    file_path = 'todo.json'
    if os.path.exists(file_path):
        # 如果文件存在，则读取文件内容
        with open(file_path, 'r') as f:
            todos = json.load(f)  # 读取现有待办事项
            print("Existing TODOs loaded from file.")
            print(todos)
            print(len(todos))
            print(type(todos))

    # 添加新的待办事项到列表中
    todos.append(new_todo)

    # 将更新后的待办事项列表写回文件
    with open(file_path, 'w') as f:
        # 将待办事项列表以JSON格式写入文件，并设置缩进为4个空格
        json.dump(todos, f, indent=4)  # 使用缩进美化JSON格式
        print("TODO added successfully.")   # 输出成功信息 
        return todos
    def delete_todo():
            # 检查文件是否存在，并相应地读取或初始化待办事项列表
        file_path = 'todo.json'
        if os.path.exists(file_path):
            # 如果文件存在，则读取文件内容
            with open(file_path, 'r') as f:
                todos = json.load(f)  # 读取现有待办事项